/*
 * Copyright 2009 Lukasz Wozniak
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *
 * http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package pl.sind.blip.entities;

import org.json.JSONException;
import org.json.JSONObject;

public class User extends Entity {
	private Long id;
	private String login;
	private String backgroundPath;
	private String avatarPath;
	private String currentStatusPath;
	private Avatar avatar;
	private Status currentStatus;

	public User() {
		super();
	}

	public User(Long id, String login, String backgroundPath,
			String avatarPath, String currentStatusPath) {
		super();
		this.id = id;
		this.login = login;
		this.backgroundPath = backgroundPath;
		this.avatarPath = avatarPath;
		this.currentStatusPath = currentStatusPath;
	}

	public User(JSONObject jsonObject) throws JSONException {
		id = jsonObject.optLong("id");
		login = jsonObject.optString("login");
		backgroundPath = jsonObject.optString("background_path");
		avatarPath = jsonObject.optString("avatar_path");
		currentStatusPath = jsonObject.optString("current_status_path");
		if (jsonObject.optJSONObject("avatar") != null) {
			avatar = new Avatar(jsonObject.optJSONObject("avatar"));
		}
		if (jsonObject.optJSONObject("current_status") != null) {
			currentStatus = StatusFactory.createStatus(jsonObject
					.optJSONObject("current_status"));
		}

	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getBackgroundPath() {
		return backgroundPath;
	}

	public void setBackgroundPath(String backgroundPath) {
		this.backgroundPath = backgroundPath;
	}

	public String getAvatarPath() {
		return avatarPath;
	}

	public void setAvatarPath(String avatarPath) {
		this.avatarPath = avatarPath;
	}

	public String getCurrentStatusPath() {
		return currentStatusPath;
	}

	public void setCurrentStatusPath(String currentStatusPath) {
		this.currentStatusPath = currentStatusPath;
	}

	public Avatar getAvatar() {
		return avatar;
	}

	public void setAvatar(Avatar avatar) {
		this.avatar = avatar;
	}

	public Status getCurrentStatus() {
		return currentStatus;
	}

	public void setCurrentStatus(Status currentStatus) {
		this.currentStatus = currentStatus;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((avatar == null) ? 0 : avatar.hashCode());
		result = prime * result
				+ ((avatarPath == null) ? 0 : avatarPath.hashCode());
		result = prime * result
				+ ((backgroundPath == null) ? 0 : backgroundPath.hashCode());
		result = prime * result
				+ ((currentStatus == null) ? 0 : currentStatus.hashCode());
		result = prime
				* result
				+ ((currentStatusPath == null) ? 0 : currentStatusPath
						.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((login == null) ? 0 : login.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (avatar == null) {
			if (other.avatar != null)
				return false;
		} else if (!avatar.equals(other.avatar))
			return false;
		if (avatarPath == null) {
			if (other.avatarPath != null)
				return false;
		} else if (!avatarPath.equals(other.avatarPath))
			return false;
		if (backgroundPath == null) {
			if (other.backgroundPath != null)
				return false;
		} else if (!backgroundPath.equals(other.backgroundPath))
			return false;
		if (currentStatus == null) {
			if (other.currentStatus != null)
				return false;
		} else if (!currentStatus.equals(other.currentStatus))
			return false;
		if (currentStatusPath == null) {
			if (other.currentStatusPath != null)
				return false;
		} else if (!currentStatusPath.equals(other.currentStatusPath))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (login == null) {
			if (other.login != null)
				return false;
		} else if (!login.equals(other.login))
			return false;
		return true;
	}

	@Override
	public String toString() {
		StringBuilder b = new StringBuilder();
		b.append(getClass().getName()).append("{");
		b.append("id: ").append(id);
		b.append(", createdAt: ").append(login);
		b.append(", backgroundPath: ").append(backgroundPath);
		b.append(", avatarPath: ").append(avatarPath);
		b.append(", avatar: ").append(avatar);
		b.append(", currentStatusPath: ").append(currentStatusPath);
		b.append(", currentStatus: ").append(currentStatus);
		b.append("}");
		return b.toString();
	}
}
